#include <memory>
#include "Protocol.hpp"
#include "Socket.hpp"
#include "InetAddr.hpp"
#include "Common.hpp"
#include "Daemon.hpp"
#include "NetCal.hpp"
#include "Tcpserver.hpp"

// ./tcpserver port
int main(int argc, char* argv[]){
	if(argc != 2){
		std::cerr<<"Usage: ./tcpserver port"<<std::endl;
		exit(USAGE_ERR);
	}

	std::cout<<"服务器启动，我已经是一个守护进程了"<<std::endl;

	Enable_File();
	Daemon(0, 0);

	uint16_t port = std::stoi(argv[1]);

	// 初始化业务逻辑层
	std::unique_ptr<Cal> cal = std::make_unique<Cal>();

	// 初始化协议层
	std::unique_ptr<Protocol> ptl = std::make_unique<Protocol>([&cal](Request& req)->Response {
		return cal->Execute(req);
	});

	// 启动服务器
	std::unique_ptr<Tcpserver> tsvr = std::make_unique<Tcpserver>(port,[&ptl](std::unique_ptr<Socket>& sock, InetAddr& client){
		
		ptl->GetRequest(sock, client);
	});

	tsvr->Start();

	return 0;
}